const sequelize = require('../config/database')
const models = require('../models')

async function syncDatabase() {
  try {
    await sequelize.authenticate()
    console.log('数据库连接成功')
    
    // 强制同步所有模型（这会删除现有表并重新创建）
    console.log('开始同步数据库模型...')
    await sequelize.sync({ force: true })
    console.log('数据库模型同步完成')
    
    // 验证products表结构
    const [results] = await sequelize.query("DESCRIBE products")
    console.log('\nproducts表结构:')
    results.forEach(column => {
      console.log(`- ${column.Field}: ${column.Type} ${column.Null === 'YES' ? '(可空)' : '(非空)'}`)
    })
    
    // 检查所有表
    const [tables] = await sequelize.query("SHOW TABLES")
    console.log('\n创建的表:')
    tables.forEach(table => {
      const tableName = table[`Tables_in_${sequelize.config.database}`]
      console.log(`- ${tableName}`)
    })
    
  } catch (error) {
    console.error('数据库同步失败:', error)
  } finally {
    await sequelize.close()
  }
}

syncDatabase()